package com.ruoyi.service.impl;

import com.ruoyi.service.IBloomFilterService;

import org.redisson.Redisson;
import org.redisson.api.RBloomFilter;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class BloomFilterServiceImpl implements IBloomFilterService {

	/**
	 * 容量
	 */

	protected Long expectedInsertions = 1000000L;

	/**
	 * 误差
	 */
	protected Double falseProbability = 0.001;


	@Resource
	private Redisson redisson;

	@Override
	public void add(String key, String data) {

		RBloomFilter bloomFilter = redisson.getBloomFilter(key);
		bloomFilter.tryInit(expectedInsertions,falseProbability);
		bloomFilter.add(data);

	}

	@Override
	public Boolean exist(String key, String data) {
		RBloomFilter bloomFilter = redisson.getBloomFilter(key);

		bloomFilter.tryInit(expectedInsertions,falseProbability);
		boolean contains = bloomFilter.contains(data);

		return contains;
	}
}
